module.exports = function ($scope, $state, $resource, $modal, str2date, getDate, $stateParams, toaster) {


	init();
	function init(){
		$scope.currentPage = 1;      //当前页码
		$scope.itemsPerPage = 10;    //每页显示几条
		$scope.searchform = {};
		$scope.saleState = {
			'new': '',
			'apply_sale': '申请上架',
			'wait_sale': '审核通过等待上架',
			'for_sale': '销售中',
			'pause': '暂停销售',
			'sale_end': '下架',
			'ban_sale': '禁止销售'
		};
		$scope.dictbytypelist = [];
		$scope.statusArr = [
			{name: '---全部---', value: ''},
			{name: '草稿', value: '0'},
			{name: '上架', value: '1'},
			{name: '下架', value: '2'}
		];
		$resource('/api/ac/tc/officeBindService/getAllList', {}, {}).save({'tag': '2'}, function (res) {
			if (res.errcode === 0) {
				$scope.channel_list = res.data.channelList;
				$scope.channel_list.unshift({ name: '--全部渠道--', code: '' });
			} else {
				toaster.error({title: "", body: res.errmsg})
			}
		});
		$resource('api/as/sc/dict/getSaleCategoryList', {}, {}).save({type: 'sale_category'}, function (res) {
			if (res.errcode !== 0) {
				toaster.error({title: "", body: res.errmsg});
				return;
			}
			$scope.dictbytypelist = res.data;
			$scope.dictbytypelist.unshift({label: '---全部---', value: ''})
		});
		$resource('/api/as/tc/placeview/jlist', {}, {}).save({type: 'D'}, function (res) {
			if (res.errcode === 0) {
				$scope.viewarr = res.data;
				$scope.viewarr.unshift({name: '--全部场所--', code: ''});
			} else {
				toaster.error({title: "", body: res.data.errmsg})
			}
		});
	}

	$scope.load = function(pageNo){
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
		};
		angular.extend(para, $scope.searchform);

		$resource('/api/ac/tc/ticketSaleService/getSale2List', {}, {}).save(para, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			$scope.noplace = res.data.results.pop();
			$scope.list = res.data.results;
			$scope.totalItems = res.data.totalRecord;
		});
	};
	$scope.load(1);

	//申请上架
	$scope.start = function (id) {
		$resource('/api/ac/tc/salehService/setup', {}, {}).save({'id': id}, function (res){
			if (res.errcode === 0) {
				$scope.load();
			} else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	}

	//下架
	$scope.stop = function (id) {
		$resource('/api/ac/tc/salehService/setdown', {}, {}).save({'id': id}, function (res){
			if (res.errcode === 0) {
				$scope.load();
			} else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	}

	$scope.edit = function (id, sale_category) {
		$resource('/api/as/sc/dict/getSaleCategoryList', {}, {}).save({ 'type': 'sale_category' }, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "/api/as/sc/dict/getSaleCategoryList", body: res.errmsg });
				return false;
			}

			var flag = false;
			for (var index = 0; index < res.data.length; index++) {
				var element = res.data[index];
				if (element.value == sale_category) {
					flag = true;
				}
			}
			if (!flag) {
				toaster.error({ title: "您的账号无权查看该类型销售品", body: "请配置销售品分类权限" });
				return false;
			}
			var modalInstance = $modal.open({
				template: require('../views/product.html'),
				controller: 'editProduct',
				url: '/product/edit/:id',
				size: 'lg',
				resolve: {
					'productid': function () {
						return id;
					},
					what: function () {
						return 'edit';
					},
					str2date: function () {
						return str2date;
					},
					date2str: function () {
						return getDate;
					},
					auditing: function () {
						return false;
					}
				}
			});
			modalInstance.result.then(function (showResult) {
				$scope.load();
			}, function (reason) {
				$scope.load();
			});
		});
	};


	$scope.asort = function (id, asort) {
		$resource('/api/ac/tc/ticketSaleService/updateSale', {}, {}).save({'id': id, 'asort': asort}, function (res){
			if (res.errcode === 0) {
				$scope.load();
			}
			else {
				toaster.error({ title: "提示", body: res.errmsg });
			}
		});
	};


	$scope.info = function (id, sale_category) {
		$resource('/api/as/sc/dict/getSaleCategoryList', {}, {}).save({ 'type': 'sale_category' }, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "/api/as/sc/dict/getSaleCategoryList", body: res.errmsg });
				return false;
			}

			var flag = false;
			for (var index = 0; index < res.data.length; index++) {
				var element = res.data[index];
				if (element.value == sale_category) {
					flag = true;
				}
			}
			if (!flag) {
				toaster.error({ title: "您的账号无权查看该类型销售品", body: "请配置销售品权限" });
				return false;
			}

			var modalInstance = $modal.open({
				template: require('../views/productInfo.html'),
				controller: 'infoProduct',
				url: '/infoProduct/:id',
				size: 'lg',
				resolve: {
					'productid': function () {
						return id;
					},
					str2date: function () {
						return str2date;
					},
					date2str: function () {
						return getDate;
					},
					auditing: function () {
						return false;
					}
				}
			});
			modalInstance.result.then(function () {
				
			});
		});
	};


	//flag:1,分配经销商
	//flga:0,不允许销售
	$scope.distribution = function (code, flag) {
		var modalInstance;
		if (flag == 1) {
			modalInstance = $modal.open({
				template: require('../views/distribution.html'),
				controller: 'distribution',
				//size: 'lg',
				resolve: {
					code: function () {
						return code;
					},
					talist: function (productservice) {
						return productservice.talist;
					},
					sellerList: function (productservice) {
						return productservice.sellerList();
					},
					tstcreate: function (productservice) {
						return productservice.tstcreate();
					},
					tststart: function (productservice) {
						return productservice.tststart();
					},
					tststop: function (productservice) {
						return productservice.tststop();
					},
					title: function () {
						return '分配经销商';
					}
				}
			});
		} else if (flag == 0) {
			modalInstance = $modal.open({
				template: require('../views/nodistribution.html'),
				controller: 'nodistribution',
				//size: 'lg',
				resolve: {
					code: function () {
						return code;
					},
					talist: function (productservice) {
						return productservice.talist;
					},
					sellerList: function (productservice) {
						return productservice.sellerListno();
					},
					tstcreate: function (productservice) {
						return productservice.tstcreateno();
					},
					tststart: function (productservice) {
						return productservice.tststartno();
					},
					tststop: function (productservice) {
						return productservice.tststopno();
					},
					title: function () {
						return '分配不允许销售经销商';
					}
				}
			});
		}
		modalInstance.result.then(function (showResult) {
			$scope.load();
		}, function (reason) {
			$scope.load();
		});

	};

	$scope.del = function (id) {
		if (confirm("您确认要删除吗？")) {
			$resource('/api/ac/tc/ticketSaleService/updateSale', {}, {}).save({'id': id, 'del_flg': '1'}, function (res) {
				if (res.errcode === 0) {
					toaster.success({ title: "", body: '删除成功' });
					$scope.load();
				}
				else {
					toaster.error({ title: "提示", body: res.errmsg });
				}
			});
		}
	};
	$scope.skset = function (id, code, name, guide_price, cost_price, productflag) {
		$scope.item = '';
		productflag = 'productflag';
		var modalInstance = $modal.open({
			template: require('../views/skprofit.html'),
			controller: 'skprofit',
			url: '/skprofit',
			size: 'lg',
			resolve: {
				item: function () {
					return $scope.item;
				},
				id: function () {
					return id;
				},
				code: function () {
					return code;
				},
				name: function () {
					return name;
				},
				guide_price: function () {
					return guide_price;
				},
				cost_price: function () {
					return cost_price;
				},
				productflag: function () {
					return productflag;
				},
				what: function () {
					return 'edit';
				},
			}
		});
		modalInstance.result.then(function () {
		});
	}

	$scope.offSale = function (id) {
		$resource('/api/as/tc/sale/offSale', {}, {}).save({ id: id }, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			toaster.success({ title: "提示", body: '修改成功!' });
			$scope.load();
		});
	}

	$scope.onSale = function (id) {
		$resource('/api/as/tc/sale/onSale', {}, {}).save({'id': id}, function (res) {
			if (res.errcode !== 0) {
				toaster.error({ title: "提示", body: res.errmsg });
				return;
			}
			toaster.success({ title: "提示", body: '修改成功!' });
			$scope.load();
		});
	}

	//设置多图-微分销用
	$scope.setImages = function (obj) {
		$resource('/api/as/wc/productshare/getshareattr', {}, {}).save({ code: obj.code }, function (res) {
			var pobj = {
				'code': obj.code,
				'title': obj.name,
			};
			var lala = {};

			if (res.errcode === 0) {
				//有数据
				// pobj['top_pic'] = res.data.top_pic;
				// pobj['words'] = res.data.words;
				// pobj['bookingnotes'] = res.data.bookingnotes;
				// pobj['detail'] = res.data.detail;
				angular.extend(res.data, pobj);
				lala = res.data;
			} else if (res.errcode === 10003) {
				//没数据
				angular.extend(lala, pobj);
			} else {
				alert(res.errmsg);
				return;
			}

			var modalInstance = $modal.open({
				template: require('../../51weshop/views/settoppic.html'),
				controller: 'weshopsettoppic',
				url: '/weshopsettoppic',
				size: 'lg',
				resolve: {
					'product': function () {
						return lala;
					},
					str2date: function () {
						return str2date;
					},
					date2str: function () {
						return getDate;
					},

				}
			});

			modalInstance.opened.then(function () {// 模态窗口打开之后执行的函数  
				// $scope.load();
			});
			modalInstance.result.then(function (showResult) {
				$scope.load();
			}, function (reason) {
				$scope.load();
				// // click，点击取消，则会暑促cancel  
				// $log.info('Modal dismissed at: ' + new Date());
			});
		});
	};


	//配置海报
	$scope.setPoster = function (obj) {
		$resource('/api/ac/wc/productShareService/getshareattr', {}, {}).save({ code: obj.code }, function (res) {
			var pobj = {
				'code': obj.code,
				'title': obj.name,
			};

			if (res.errcode === 0) {
				//没有数据
				if (res.data.xxx || res.data.xxx == 'yyy') {

				}
				//有数据
				else {
					pobj['id'] = obj.id;
					if (res.data.img) {
						pobj['share_pic'] = res.data.img;
					}
					if (res.data.qrcode_size) {
						pobj['qrcode_size'] = res.data.qrcode_size;
					}
					if (res.data.width) {
						pobj['width'] = res.data.width;
					}
					if (res.data.height) {
						pobj['height'] = res.data.height;
					}
					if (res.data.nickname_width) {
						pobj['nickname_width'] = res.data.nickname_width;
					}
					if (res.data.nickname_height) {
						pobj['nickname_height'] = res.data.nickname_height;
					}
					if (res.data.nickname_size) {
						pobj['nickname_size'] = res.data.nickname_size;
					}
					if (res.data.nickname_color) {
						pobj['nickname_color'] = res.data.nickname_color;
					}
					if (res.data.nickname_length) {
						pobj['nickname_length'] = res.data.nickname_length;
					}
				}
			} else if (res.errcode === 9999) {
				//没数据

			} else {
				alert(res.errmsg);
				return;
			}

			var modalInstance = $modal.open({
				template: require('../../51weshop/views/setposter.html'),
				controller: 'weshopposter',
				url: '/weshopposter',
				size: 'lg',
				resolve: {
					'obj': function () {
						return pobj;
					},
					str2date: function () {
						return str2date;
					},
					date2str: function () {
						return getDate;
					}
				}
			});
			modalInstance.result.then(function (showResult) {
				$scope.load();
			}, function (reason) {
				$scope.load();
			});
		});

	};
    
	//设置公开范围
	$scope.change2 = function (info, flag, index){
		var para = {'sale_code': info.code};
		var msg = info.open_company == '1'? '确认修改为公开销售吗?': '修改后将删除已授权的分销商，确认修改为单独授权销售吗?';
        para.open_company = flag? '0': '1';
		if(confirm(msg) == true){
			$resource('/api/ac/tc/ticketSaleAuthService/updateOpen', {}, {}).save(para, function (res) {
				if (res.errcode === 0 && res.data.msg === 'success') {
					toaster.success({title: '', body: '修改成功'});
					$scope.load();
				} else {
					toaster.error({title: '', body: '修改失败'});
                    $scope.objs[index].is_selected3 = !flag;
				}
			});
		} else {
            $scope.objs[index].is_selected3 = !flag;
		}
	}

	$scope.setFxs = function (item) {
		var modalInstance = $modal.open({
			template: require('../views/setFxs.html'),
			controller: 'setFxs',
			size: 'lg',
			resolve: {
				item : function () {
					return item;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.load();
		});
	}
	//设置渠道商
    $scope.setChannel = function (item){
        var modalInstance = $modal.open({
			template: require('../views/setChannel.html'),
			controller: 'setChannel',
			size: 'lg',
			resolve: {
				sale_code : function () {
					return item.code;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
			$scope.load();
		});
	}
	
	//跳转我的渠道
	$scope.myChannel = function (item) {
		$state.go('app.myChannel', {});
	}

	//景区介绍
	$scope.introduce = function(place_code){
		var modalInstance = $modal.open({
			template: require('../../33sale/views/placeDetail.html'),
			controller: 'placeDetail',
			size: 'lg',
			resolve: {
				place_code : function(){
					return place_code;
				}
			}
		});
	};

	//查看图片
	$scope.lookImg = function (url) {
		var modalInstance = $modal.open({
			template: require('../../33sale/views/img.html'),
			controller: 'img',
			size: 'lg',
			resolve: {
				img: function () {
					return url;
				}
			}
		});
		modalInstance.result.then(function (result) {
		});
	}

};